﻿using Developer.Service;
using Developer.Web.Areas.Admin.Controllers;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Controllers
{
    public class FixController : BaseController
    {
        // GET: ReRepair
        public ActionResult Index()
        {
            var Repairlist = RepairTypeService.GetAll().Where(a => a.IsAbort == false);

            return View(Repairlist);
        }


        public ActionResult SelectBrand(int? tid)
        {
            var brandlist = RepairBrandService.GetAll().Where(a => a.RepairTypeId == tid && a.IsAbort == false);
            return View(brandlist);
        }

        public ActionResult SelectModel(int? bid)
        {
            var modellist = RepairModelService.GetAll().Where(a => a.RepairBrandId == bid && a.IsAbort == false);
            return View(modellist);
        }

        public ActionResult SelectProblem(int? mid)
        {
           
                var problemlist = RepairProblemService.GetAll().Where(a => a.RepairColorId == mid && a.IsAbort == false);
                ViewBag.device = problemlist.FirstOrDefault().RepairColor.RepairModel.Name + " " + problemlist.FirstOrDefault().RepairColor.Name;
                return View(problemlist);
        }

        [HttpPost]
        public ActionResult GetPrice(int? mid)
        {
            var info = RepairColorService.GetAll().Find(mid);
            //Session["mid"] = mid;
            //Session["totalProblem"] = Query.Value("totalProblem");
            ViewBag.problem = Query.Value("totalProblem");
            var totalPrice = 0;
            var radioCount = 0;
            ViewBag.mid = mid;
            var checkboxCount = 0;
            foreach (var item in info.RepairProblem)
            {
                if (item.Name.Contains("(") || item.Name.Contains("（"))
                {
                    for (int i = 0; i < item.RepairProblemDetail.Count; i++)
                    {
                        checkboxCount++;

                    }
                }
                else
                {
                    radioCount++;
                }
            }
            var radio = new int[radioCount];
            var checkbox = new int[checkboxCount];
            for (int i = 0; i < radioCount; i++)
            {
                radio[i] = Query.Value("radio" + (i + 1)).ToInt();
                totalPrice += radio[i];
            }
            for (int i = 0; i < checkboxCount; i++)
            {
                checkbox[i] = Query.Value("checkbox" + (i + 1)).ToInt();
                totalPrice += checkbox[i];
            }
            ViewBag.totalPrice = totalPrice;
            //Session["totalPrice"] = totalPrice;
            ViewBag.phone = info.Name;
            var statistic = RepairColorService.GetAll().Find(mid);
            statistic.Statistic++;
            RepairColorService.Change(statistic);
            return View();
        }
        [HttpPost]
        public ActionResult GetOrder()
        {
            string problem = Query.Value("problem");
            int price = Query.Value("price").ToInt();
            int mid = Query.Value("mid").ToInt();
            ViewBag.problem = problem;
            ViewBag.price = price;
            ViewBag.mid = mid;
            return View();
        }
        [HttpPost]
        public ActionResult SureOrder()
        {
            string problem = Query.Value("problem");
            int price = Query.Value("price").ToInt();
            int mid = Query.Value("mid").ToInt();
            //var mid = Session["mid"].ToString().ToInt();
            //var totalProblem = Session["totalProblem"].ToString();
            var donetime = Query.Value("datetime1") + " " + Query.Value("datetime2");
            var name = Query.Value("name");
            var phone = Query.Value("phone");
            var address = Query.Value("address1") + Query.Value("address2");
            var Num = GetOrderNum();
            RepairOrderDetailService.Add(new Entity.RepairOrderDetail
            {
                RepairColorId = mid,
                Problem = problem,
                UserPhone = phone,
                UserAddress = address,
                UserName = name,
                Num = Num,
                ReferPrice = price
            });

            var codId = RepairOrderDetailService.GetAll().FirstOrDefault(a => a.Num == Num).Id;
            RepairOrderService.Add(new Entity.RepairOrder
            {
                CreateTime = DateTime.Now,
                DoneTime = DateTime.Parse(donetime),
                State = 0,
                Num = Num,
                RepairOrderDetailId = codId,
            });
            var smsContent = "http://www.jixunxiu.com/fix/getorderdetail4admin?odId=" + codId + "&price=" + price;
            smsContent = "【蚁行软件】您当前有新的用户维修订单，后台地址" + smsContent;
            var admins = AdminerService.GetAll().Where(a => a.Identity < 0);
            try
            {
                foreach (var item in admins)
                {
                    var a = SMSService.SendSms(Uri.EscapeDataString(smsContent), item.Phone);
                    Console.WriteLine(a);
                }
            }
            catch (Exception e)
            {

            }
            return View();
        }

        [HttpPost]
        public ActionResult SaveVideo()
        {
            var url = Query.Value("video");
            var odId = Query.Value("odId").ToInt();
            var price = Query.Value("price").ToInt();

            var rod = RepairOrderDetailService.GetAll().Find(odId);
            rod.VideoUrl = url;
            RepairOrderDetailService.Change(rod);
            return Redirect("/fix/GetOrderDetail4Admin?odId=" + odId + "&price=" + price);
        }
        public ActionResult SetVideo(int? odId, int price)
        {
           
            var rod = RepairOrderDetailService.GetAll().Find(odId);
            ViewBag.price = price;
            return View(rod);
        }

        public ActionResult test()
        {
            //var ps = RepairProblemService.GetAll().GroupBy(a => a.RepairColorId);
            //foreach(var item in ps)
            //{
            //    int t = 1;
            //    foreach(var i in item)
            //    {

            //        i.Sequence = t++;
            //    }
            //}
            //SysInfo.DB.SaveChanges();
            //var ps2 = CycleProblemDetailService.GetAll().GroupBy(a => a.CycleProblemId);
            //foreach (var item in ps2)
            //{
            //    int t = 1;
            //    foreach (var i in item)
            //    {

            //        i.Sequence = t++;
            //    }
            //}
            //SysInfo.DB.SaveChanges();

            var ps3 = CycleProblemService.GetAll().GroupBy(a => a.CycleModelId);
            foreach (var item in ps3)
            {
                int t = 1;
                foreach (var i in item)
                {

                    i.Sequence = t++;
                }
            }
            SysInfo.DB.SaveChanges();
            return View();
        }


        public ActionResult GetOrderDetail4Admin(int odId, int? price)
        {
            var orderd = RepairOrderDetailService.GetAll().Find(odId);
            ViewBag.price = price;
            return View(orderd);
        }
        public static object _lock = new object();
        public static int count = 1;
        public string GetOrderNum()
        {
            lock (_lock)
            {
                if (count >= 10000)
                {
                    count = 1;
                }
                var number = "P" + DateTime.Now.ToString("yyMMddHHmmss") + count.ToString("0000");
                count++;
                return number;
            }
        }
    }
}